<?php
        session_start();
        if($_SESSION["username"] == "")
        {
                header("location:login.php");
                exit();
        }
        include 'connectDB.php';

        $strSQL = "SELECT * FROM tbl_user WHERE username = '".$_SESSION["username"]."'  ";
        $objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
        $objResult = mysql_fetch_array($objQuery);
?>
<html>

<head>
  <style>
        input:focus {
          box-shadow: 0 0 15px 5px #b0e0ee;
	         border: 2px solid #bebede;
         }
				a:hover {
					box-shadow: 0 0 15px 5px #b0e0ee;
					color:	#ffb6ad;
				}
</style>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <script src="js/jquery-1.9.1.js"></script>
        <script src="js/check_textbox.js"></script>
        <script>
                //$("#frmAddItem").hide;
                //$("#frmAddItem").css("display","none");
                $(document).ready(function(){
                  $("#panel").hide();
                  $("#panel_edit").hide();
                  $("#add").click(function(){
                  $("#panel").slideToggle("slow");
                });
                        $.ajax({
                                type: "POST",
                                url: "item_service.php",
                                data: {function:'show_table_1'} ,
                                success: function(data) {
                                        $("#table_show").html(data);
                                }
                        });


                });

                function edit_item_save(){
                        var item_id =$("#item_id").val();
                        var item_name = $("#txtItemName_e").val();
                        var price = $("#txtPrice_e").val();
                        var mer_id = $("#merchant_ddl_e").val();
                        if(piv(price) &&item_name!=''&& price!=''){
                                        $.ajax({
                                                type: "POST",
                                                url: "item_service.php",
                                                data: {function:'edit_item_save', item_id: item_id,item_name:item_name,price:price,mer_id:mer_id} ,
                                                success: function(data) {
                                                        $("#table_show").html(data);
                                                        $("#panel_edit").fadeOut("slow");
                                                }
                                        });
                                }
                                else{
                                        alert('Wrong Format');
                                }
                }
                function edit_item(id,name,price,mer_id){
                        $("#item_id").val(id);
                        var item_id=$("#item_id").val();
                        $("#panel_edit").fadeIn("slow");
                        $("#txtItemName_e").val(name);
                        $("#txtPrice_e").val(price);
                        $("#merchant_ddl_e").val(mer_id);
                }

                function  del_item(id){
                        $("#item_id").val(id);
                        var item_id=$("#item_id").val();
                        $.ajax({
                                type: "POST",
                                url: "item_service.php",
                                data: {function:'item_del', item_id: item_id} ,
                                success: function(data) {
                                        $("#table_show").html(data);
                                }
                        });
                }
                function save_item(){
                        var txtItemName = $("#txtItemName").val();
                        var txtPrice = $("#txtPrice").val();
                        var merchant = $("#merchant_ddl").val();
                        if(piv(txtPrice) && txtPrice!='' && txtItemName!=''){
                                //alert(txtItemName+txtPrice+merchant);
                                $.ajax({
                                        type: "POST",
                                        url: "item_service.php",
                                        data: {function:'item_add', txtItemName: txtItemName,txtPrice:txtPrice,merchant:merchant} ,
                                        success: function(data) {
                                                //alert(data);
                                                $("#table_show").html(data);
                                                $("#panel").slideUp();
                                        }
                                });
                        }
                        else{
                                alert('Wrong Format');
                        }


		}
		function check_real_time(value,span){
			if(piv(value) && value != ''){
				$(span).html("True");
			}
			else{
				$(span).html("Wrong Format");
			}
		}



        </script>
<title>Welcome to Item Manage.</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>


<body>


<div id="header">
        <div class="logo">Item Manage</div>
        <div class="user" >Hi !  <?=$objResult["username"];?><br><a href="logout.php" name="logout" id="logout" />logout</a></div>
</div>
<div id="container">
        <a href="dashboard.php">dashboard</a> > <a href="">item</a>
        <br>
        <br>

        <div style="height:41px;width:100px;" id="add">
<img id ="image" src="image/button.jpg"/>
        </div>
        <input type="hidden" id="item_id"/>
        <div id="panel">
        <form  id="frmAddItem" name="frmAddItem"  method="post">
        <h2>Item Add</h2>
        <table border="0 " cellpadding="10" cellspacing="0" style="margin-left: 50px;">
                <tr>
                        <td>Item Name :</td>
                        <td><input style="border-bottom-color:#CCC8C0;" type="text" id="txtItemName" name="txtItemName" style="padding: 10px;"/></td>
                </tr>
                <tr>
                        <td>Price :</td>
                        <td><input style="border-bottom-color:#CCC8C0;" type="text" onkeyup="check_real_time(this.value,alert_1)" id="txtPrice" name="txtPrice" style="padding: 10px;"/><span style="color:pink" id="alert_1"></span></td>
                </tr>

                <tr>
                        <td>Merchant Name:</td>
                        <td><select id="merchant_ddl" name="merchant_ddl">
                <?php
                                $strSQL = "SELECT * FROM tbl_merchant ";
                                $objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");

                                while ($objResult = mysql_fetch_array($objQuery))
                                {
                                        $variable1 = $objResult["mer_name"];
                                        $variable2 = $objResult["mer_id"];
                                        echo "<OPTION VALUE=\"$variable2\">".$variable1.'</option>';
                                }
                ?>
                        </select></td>
                </tr>

                <tr>
                        <td></td>
                        <td><input type="button" name="submit" value="submit" onclick="save_item()" style="padding: 10px;"> <input type="reset" name="reset" value="reset" style="padding: 10px;"/></td>
                </tr>
        </table>
        </form>
        </div>


<div id="panel_edit">
<form  id="frmEditItem" name="frmEditItem"  method="post">
<h2>Item Edit</h2>
<table border="0 " cellpadding="10" cellspacing="0" style="margin-left: 50px;">
        <?php

        ?>

        <tr>
                <td>Item Name :</td>
                <td><input style="border-bottom-color:#CCC8C0;" type="text" id="txtItemName_e" name="txtItemName_e" style="padding: 10px;"/></td>
        </tr>
        <tr>
                <td>Price :</td>
                <td><input style="border-bottom-color:#CCC8C0;" type="text" onkeyup="check_real_time(this.value,alert_2)"  id="txtPrice_e" name="txtPrice_e" style="padding: 10px;"/><span style="color:pink" id="alert_2"></span></td>
        </tr>

        <tr>
                <td>Merchant Name:</td>
                <td><select id="merchant_ddl_e" name="merchant_ddl_e">
        <?php
                        $strSQL = "SELECT * FROM tbl_merchant ";
                        $objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");

                        while ($objResult = mysql_fetch_array($objQuery))
                        {
                                $variable1 = $objResult["mer_name"];
                                $variable2 = $objResult["mer_id"];
                                echo "<OPTION VALUE=\"$variable2\">".$variable1.'</option>';
                        }
        ?>
                </select></td>
        </tr>

        <tr>
                <td></td>
                <td><input type="button" name="submit_e" value="submit" onclick="edit_item_save()" style="padding: 10px;"> <input type="reset" name="reset" value="reset" style="padding: 10px;"/></td>
        </tr>
</table>
</form>
</div>

        <br>
        <br>


        <div id="table_show"></div>

</form>
</body>
</html>
